#ifdef _IEXPLORER
#include "webbot_ie.hpp"
#else 
#include "moz_mechanize.hpp"
#include <iostream>
#endif

#include <sstream>
#include <stdlib.h>

#ifdef _IEXPLORER
#define MIN_ARGS 3
#else
#define MIN_ARGS 5
#endif

void help(const char* argv)
{
#ifdef _IEXPLORER
	std::cout << "Usage: " << argv << " -c profile_filename " << std::endl;
#else
	std::cout << "Usage: " << argv << " -c profile_filename -p profile_num [-m moz_repl_config]" << std::endl;
#endif
}

int main(int argc, const char* argv[])
{
	std::string conf, moz_conf;
	int profile_num = -1;

	std::stringstream id;

	if(argc < MIN_ARGS)
	{
		help(argv[0]), exit(EXIT_FAILURE);
	}
		
	for(unsigned short j =1; j < argc; j++)
	{
		if(argv[j][0] == '-' || 
			argv[j][0] == '/')
			switch(argv[j][1])
			{			
				case 'c':
					if(j < argc -1)
						conf = argv[j+1];
					break;
				case 'm':
					if(j < argc -1)
						moz_conf = argv[j+1];
					break;
				case 'p':
						id << argv[j+1];
						id >> profile_num;
						if(id.rdbuf()->in_avail() == 0) // it is a number
							break;
				default: 
					help(argv[0]);
					break;
			}			   
	}

	if(conf.empty())
	{
		help(argv[0]), exit(EXIT_FAILURE);
	}

#ifdef _IEXPLORER
	iwebbot* rob = new webbot_ie(conf.c_str());	
#else
	
	if((id.rdbuf()->in_avail() != 0) || profile_num < 0) 
		help(argv[0]), exit(EXIT_FAILURE);

	const char* moz_repl_config = moz_conf.empty() ? NULL : moz_conf.c_str();

	moz_mechanize* rob = new moz_mechanize(conf.c_str(), profile_num, moz_repl_config);
#endif

	rob->dispatch();	

	delete rob;

}


 
